package ink.sunflowerk.controller;

import ink.sunflowerk.pojo.Teacher;
import ink.sunflowerk.service.TeacherService;
import ink.sunflowerk.util.EmailUtils;
import ink.sunflowerk.util.ScheduleSessionUtil;
import ink.sunflowerk.util.SendPhoneMessageUtils;
import ink.sunflowerk.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

/**
 * @author zhaokuii11@163.com
 * @create 2021-12-09 9:29
 * @Description
 */
@Controller
@RequestMapping("/teacher")
public class TeacherAction {
    @Autowired
    TeacherService teacherService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        Teacher login = teacherService.login(username, password);
        session.setAttribute("teacher", login);
        System.out.println(login);
        return "redirect:/student/show";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login.jsp";
    }

    @RequestMapping("/loginPhone")
    public String login(String phone, HttpSession session) {
        Teacher teacher = teacherService.login(phone);
        session.setAttribute("teacher", teacher);
        return "redirect:/student/show";
    }

    @RequestMapping("/register")
    public String login(String salutation, String phone, String email, String name, String pwd) {
        Teacher teacher = new Teacher(salutation, phone, email, name, pwd);
        teacherService.register(teacher);
        return "redirect:/login.jsp";
    }


    @RequestMapping("/getImage")
    public void getImage(HttpSession session, HttpServletResponse response) {
        //生成验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        System.out.println("登录验证码 " + code);
        //验z证码放入 session
        session.setAttribute("imgCode", code);
        //验证码存入图片
        try {
            response.setContentType("image/png");
            VerifyCodeUtils.outputImage(200, 60, response.getOutputStream(), code);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/checkImgCode")
    public void checkImgCode(String tcode,
                             HttpServletRequest request,
                             HttpServletResponse response) throws IOException {
        String checkCode = (String) request.getSession().getAttribute("imgCode");
        //1 成功 0 失败
        if (checkCode.equalsIgnoreCase(tcode)) {
            response.getWriter().print(1);
        } else {
            response.getWriter().print(0);
        }
    }

    @RequestMapping("/sendEmail")
    public void sendEmail(String email,
                          HttpSession session,
                          HttpServletResponse response
    ) throws Exception {
        //获取6位验证码
        String emailCode = VerifyCodeUtils.generateVerifyCode(6);
        System.out.println("短信验证码 = " + emailCode);
        //发送邮箱短信
        EmailUtils.SendEmail(email, emailCode);
        session.setAttribute("emailCode", emailCode);
        //设置存货时间
        ScheduleSessionUtil.removeAttribute(session, "emailCode", 1);
        response.getWriter().print(1);
    }

    @RequestMapping("/checkEmailCode")
    public void checkEmailCode(String ecode,
                               HttpSession session,
                               HttpServletResponse response) throws IOException {
        String checkCode = (String) session.getAttribute("emailCode");
        //1 成功 0 失败
        if (Objects.equals(checkCode, ecode)) {
            response.getWriter().print(1);
        } else {
            response.getWriter().print(0);
        }
    }

    @RequestMapping("/sendPhoneMsg")
    public void sendPhoneMsg(String phone,
                             HttpSession session,
                             HttpServletResponse response) throws IOException {
        //获取6位验证码
        String phoneCode = VerifyCodeUtils.generateVerifyCode(6);
        System.out.println("短信验证码 = " + phoneCode);
        session.setAttribute("phoneCode", phoneCode);
        //发送手机短信
        //SendPhoneMessageUtils.sendMessage(phoneCode,phone);
        //设置存货时间
        ScheduleSessionUtil.removeAttribute(session, "phoneCode", 1);
        response.getWriter().print(1);
    }

    @RequestMapping("/checkPhoneCode")
    public void checkPhoneCode(String phoneCode,
                               HttpSession session,
                               HttpServletResponse response) throws IOException {
        String checkCode = (String) session.getAttribute("phoneCode");
        //1 成功 0 失败
        if (Objects.equals(checkCode, phoneCode)) {
            response.getWriter().print(1);
        } else {
            response.getWriter().print(0);
        }
    }
}
